package com.zw.cloud.influxdb.controller;
import java.math.BigDecimal;

import com.zw.cloud.influxdb.entity.MonitorDeviceRunInfoFlink;
import com.zw.cloud.influxdb.service.InfluxdbQueryService;
import lombok.extern.slf4j.Slf4j;
import org.influxdb.InfluxDB;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/influxdb/device-info")
public class MonitorDeviceRunInfoFlinkController {

    @Autowired
    private InfluxdbQueryService influxdbQueryService;
    @Autowired
    private InfluxDB influxDBClient;

    /**
     * 获取数据
     */
    @GetMapping("/queryList/{deviceId}")
    //http://localhost:10010/influxdb/device-info/queryList/3983D94F788342E1B0D8F52EA890D8E8
    public List<MonitorDeviceRunInfoFlink> queryList(@PathVariable String deviceId){
        String runInfoSql = "SELECT * FROM monitor_device_run_info_flink WHERE time > now() - 11m and deviceId = $deviceId ";
        Map<String, Object> bindParams = new HashMap<>();
        bindParams.put("deviceId", deviceId);
        return influxdbQueryService.queryDeviceReportData(runInfoSql,MonitorDeviceRunInfoFlink.class,bindParams);
    }

    /**
     * 批量插入
     */
    @GetMapping("/batchInsert/{orgCode}/{deviceId}/{minusMinutes}")
    //http://localhost:10010/influxdb/device-info/batchInsert/3983D94F788342E1B0D8F52EA890D8E8/1/10
    public void batchInsert(@PathVariable String orgCode,@PathVariable String deviceId, @PathVariable Integer minusMinutes) {
        LocalDateTime now = LocalDateTime.now();
        List<MonitorDeviceRunInfoFlink> deviceRunInfoFlinkList = new ArrayList<>();
        for (int i = 0; i < minusMinutes; i++) {
            MonitorDeviceRunInfoFlink deviceRunInfoFlink = buildMonitorDeviceRunInfoFlink(orgCode,deviceId,BigDecimal.valueOf(30+i), now.minusMinutes(i));
            deviceRunInfoFlinkList.add(deviceRunInfoFlink);
        }
        deviceRunInfoFlinkList.sort(Comparator.comparing(MonitorDeviceRunInfoFlink::getTime));
        BatchPoints.Builder builder = BatchPoints.builder();
        deviceRunInfoFlinkList.forEach(m -> builder.point(Point.measurementByPOJO(m.getClass()).addFieldsFromPOJO(m).build()));
        //写入
        influxDBClient.write(builder.build());
    }

    private MonitorDeviceRunInfoFlink buildMonitorDeviceRunInfoFlink(String orgCode, String deviceId, BigDecimal actuallyValue,LocalDateTime localDateTime) {
        Long second = localDateTime.toEpochSecond(ZoneOffset.of("+8"));
        Instant instant = Instant.ofEpochSecond(second);

        MonitorDeviceRunInfoFlink deviceRunInfoFlink = new MonitorDeviceRunInfoFlink();
        deviceRunInfoFlink.setTime(instant);
        deviceRunInfoFlink.setOrgCode(orgCode);
        deviceRunInfoFlink.setDeviceId(deviceId);
        deviceRunInfoFlink.setTargetCode("lowerFeed");
        deviceRunInfoFlink.setActuallyValue(actuallyValue);
        deviceRunInfoFlink.setAlertLevel(0);
        deviceRunInfoFlink.setAlertStartTime(second);
        deviceRunInfoFlink.setAlertType("d27");
        deviceRunInfoFlink.setAlertTypeName("下超喂");
        deviceRunInfoFlink.setCloudTime(second);
        deviceRunInfoFlink.setCtime(second);
        deviceRunInfoFlink.setDeviceCode("004");
        deviceRunInfoFlink.setDeviceName("定型机2");
        deviceRunInfoFlink.setGenerate(0);
        deviceRunInfoFlink.setIcCard("231106222");
        deviceRunInfoFlink.setIotCode("8888");
        deviceRunInfoFlink.setSettingVal(false);
        deviceRunInfoFlink.setRecommendValue(new BigDecimal("20"));
        deviceRunInfoFlink.setSettingValue(new BigDecimal("30"));
        return deviceRunInfoFlink;
    }

}
